package cool.taomu.toolkit.quartz.module

import com.google.inject.AbstractModule
import com.google.inject.matcher.Matchers
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.quartz.QuartzMethodInterceptor
import cool.taomu.toolkit.quartz.ScheduledJob
import cool.taomu.toolkit.utils.ReflectionsScanner
import org.quartz.Scheduler
import org.quartz.impl.StdSchedulerFactory
import org.reflections.scanners.Scanners
import org.slf4j.LoggerFactory

class QuartzModule extends AbstractModule {
	val LOG = LoggerFactory.getLogger(QuartzModule)

	new(TaomuConfigure config) {
	}

	override configure() {
		bind(Scheduler).toInstance(StdSchedulerFactory.getDefaultScheduler());
		var qmi = new QuartzMethodInterceptor()
		requestInjection(qmi);
		bindInterceptor(
			Matchers.any(),
			Matchers.annotatedWith(ScheduledJob),
			qmi
		);
		var a = ReflectionsScanner.scanType(ScheduledJob, "cool.taomu.toolkit", Scanners.MethodsAnnotated)
		a.forEach [
			LOG.debug(it.name)
			bind(it)
		]
	}
}
